package lockstrategy;

import java.util.Hashtable;
import java.util.concurrent.CountDownLatch;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-05-29
 * Time: 0:15
 */
public class Demo6 {
    public static void main(String[] args) {
        // 构造方法的参数表示有几个选手参赛
        CountDownLatch latch = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(()->{
                try {
                    Thread.sleep(3000);
                    System.out.println(Thread.currentThread().getName()+"到达终点！");
                    //表示达到终点了
                    latch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            t.start();
        }
        // 裁判要等待所有的线程到达终点
        //当这些线程没有执行完的时候， await 就会阻塞，直到所有的线程都执行完了，await 才返回
        try {
            latch.await();
            System.out.println("比赛结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
